package com.tang.common.utils.excel;

import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.style.AbstractCellStyleStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * @Author tang jun liang
 * @Date 2023/10/25 09:11
 */
public class ExcelStyleBoldHandler extends AbstractCellStyleStrategy {

    @Override
    protected void setContentCellStyle(CellWriteHandlerContext context) {
        Cell cell = context.getCell();
        int columnIndex = cell.getColumnIndex();
        // 科目编号为三位时，将前两列加粗
        if(columnIndex == 0 ) {
            String stringCellValue = cell.getStringCellValue();
            if (stringCellValue.length() == 3) {
                Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
                Font font = workbook.getFontAt(0);
                CellStyle cellStyle = cell.getCellStyle();
                font.setBold(true);
                cellStyle.setFont(font);
                cell.setCellStyle(cellStyle);
            }
        }
        if (columnIndex == 1) {
            Cell prevCell = context.getRow().getCell(columnIndex - 1);
            String prevStringCellValue = prevCell.getStringCellValue();
            if(prevStringCellValue.length() == 3) {
                cell.setCellStyle(prevCell.getCellStyle());
            }
        }
    }
}
